<!-- Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<title>Unit test of e2e.scheme.rsaes</title>
<script src="../../../closure/base.js"></script>
<script src="test_js_deps-runfiles.js"></script>
<script>
  goog.require('e2e.cipher.RSA');
  goog.require('e2e.scheme.Rsaes');
  goog.require('goog.testing.asserts');
  goog.require('goog.testing.jsunit');
</script>
<script>
  var RSA_PUB_DER = [
   0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06,
   0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
   0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
   0x03, 0x81, 0x8d, 0x00, 0x30, 0x81,
   0x89, 0x02, 0x81, 0x81, 0x00, 0xa5,
   0x6e, 0x4a, 0x0e, 0x70, 0x10, 0x17,
   0x58, 0x9a, 0x51, 0x87, 0xdc, 0x7e,
   0xa8, 0x41, 0xd1, 0x56, 0xf2, 0xec,
   0x0e, 0x36, 0xad, 0x52, 0xa4, 0x4d,
   0xfe, 0xb1, 0xe6, 0x1f, 0x7a, 0xd9,
   0x91, 0xd8, 0xc5, 0x10, 0x56, 0xff,
   0xed, 0xb1, 0x62, 0xb4, 0xc0, 0xf2,
   0x83, 0xa1, 0x2a, 0x88, 0xa3, 0x94,
   0xdf, 0xf5, 0x26, 0xab, 0x72, 0x91,
   0xcb, 0xb3, 0x07, 0xce, 0xab, 0xfc,
   0xe0, 0xb1, 0xdf, 0xd5, 0xcd, 0x95,
   0x08, 0x09, 0x6d, 0x5b, 0x2b, 0x8b,
   0x6d, 0xf5, 0xd6, 0x71, 0xef, 0x63,
   0x77, 0xc0, 0x92, 0x1c, 0xb2, 0x3c,
   0x27, 0x0a, 0x70, 0xe2, 0x59, 0x8e,
   0x6f, 0xf8, 0x9d, 0x19, 0xf1, 0x05,
   0xac, 0xc2, 0xd3, 0xf0, 0xcb, 0x35,
   0xf2, 0x92, 0x80, 0xe1, 0x38, 0x6b,
   0x6f, 0x64, 0xc4, 0xef, 0x22, 0xe1,
   0xe1, 0xf2, 0x0d, 0x0c, 0xe8, 0xcf,
   0xfb, 0x22, 0x49, 0xbd, 0x9a, 0x21,
   0x37, 0x02, 0x03, 0x01, 0x00, 0x01
  ];
  var RSA_KEY = {
    'n': [0x00, 0xa5,
   0x6e, 0x4a, 0x0e, 0x70, 0x10, 0x17,
   0x58, 0x9a, 0x51, 0x87, 0xdc, 0x7e,
   0xa8, 0x41, 0xd1, 0x56, 0xf2, 0xec,
   0x0e, 0x36, 0xad, 0x52, 0xa4, 0x4d,
   0xfe, 0xb1, 0xe6, 0x1f, 0x7a, 0xd9,
   0x91, 0xd8, 0xc5, 0x10, 0x56, 0xff,
   0xed, 0xb1, 0x62, 0xb4, 0xc0, 0xf2,
   0x83, 0xa1, 0x2a, 0x88, 0xa3, 0x94,
   0xdf, 0xf5, 0x26, 0xab, 0x72, 0x91,
   0xcb, 0xb3, 0x07, 0xce, 0xab, 0xfc,
   0xe0, 0xb1, 0xdf, 0xd5, 0xcd, 0x95,
   0x08, 0x09, 0x6d, 0x5b, 0x2b, 0x8b,
   0x6d, 0xf5, 0xd6, 0x71, 0xef, 0x63,
   0x77, 0xc0, 0x92, 0x1c, 0xb2, 0x3c,
   0x27, 0x0a, 0x70, 0xe2, 0x59, 0x8e,
   0x6f, 0xf8, 0x9d, 0x19, 0xf1, 0x05,
   0xac, 0xc2, 0xd3, 0xf0, 0xcb, 0x35,
   0xf2, 0x92, 0x80, 0xe1, 0x38, 0x6b,
   0x6f, 0x64, 0xc4, 0xef, 0x22, 0xe1,
   0xe1, 0xf2, 0x0d, 0x0c, 0xe8, 0xcf,
   0xfb, 0x22, 0x49, 0xbd, 0x9a, 0x21,
   0x37],
    'e': [0x01, 0x00, 0x01],
    'd': [0x33,
   0xa5, 0x04, 0x2a, 0x90, 0xb2, 0x7d,
   0x4f, 0x54, 0x51, 0xca, 0x9b, 0xbb,
   0xd0, 0xb4, 0x47, 0x71, 0xa1, 0x01,
   0xaf, 0x88, 0x43, 0x40, 0xae, 0xf9,
   0x88, 0x5f, 0x2a, 0x4b, 0xbe, 0x92,
   0xe8, 0x94, 0xa7, 0x24, 0xac, 0x3c,
   0x56, 0x8c, 0x8f, 0x97, 0x85, 0x3a,
   0xd0, 0x7c, 0x02, 0x66, 0xc8, 0xc6,
   0xa3, 0xca, 0x09, 0x29, 0xf1, 0xe8,
   0xf1, 0x12, 0x31, 0x88, 0x44, 0x29,
   0xfc, 0x4d, 0x9a, 0xe5, 0x5f, 0xee,
   0x89, 0x6a, 0x10, 0xce, 0x70, 0x7c,
   0x3e, 0xd7, 0xe7, 0x34, 0xe4, 0x47,
   0x27, 0xa3, 0x95, 0x74, 0x50, 0x1a,
   0x53, 0x26, 0x83, 0x10, 0x9c, 0x2a,
   0xba, 0xca, 0xba, 0x28, 0x3c, 0x31,
   0xb4, 0xbd, 0x2f, 0x53, 0xc3, 0xee,
   0x37, 0xe3, 0x52, 0xce, 0xe3, 0x4f,
   0x9e, 0x50, 0x3b, 0xd8, 0x0c, 0x06,
   0x22, 0xad, 0x79, 0xc6, 0xdc, 0xee,
   0x88, 0x35, 0x47, 0xc6, 0xa3, 0xb3,
   0x25]
  };
  var RSA_SEC_DER = [
   0x30, 0x82, 0x02, 0x75, 0x02, 0x01,
   0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
   0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
   0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
   0x02, 0x5f, 0x30, 0x82, 0x02, 0x5b,
   0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
   0x00, 0xa5, 0x6e, 0x4a, 0x0e, 0x70,
   0x10, 0x17, 0x58, 0x9a, 0x51, 0x87,
   0xdc, 0x7e, 0xa8, 0x41, 0xd1, 0x56,
   0xf2, 0xec, 0x0e, 0x36, 0xad, 0x52,
   0xa4, 0x4d, 0xfe, 0xb1, 0xe6, 0x1f,
   0x7a, 0xd9, 0x91, 0xd8, 0xc5, 0x10,
   0x56, 0xff, 0xed, 0xb1, 0x62, 0xb4,
   0xc0, 0xf2, 0x83, 0xa1, 0x2a, 0x88,
   0xa3, 0x94, 0xdf, 0xf5, 0x26, 0xab,
   0x72, 0x91, 0xcb, 0xb3, 0x07, 0xce,
   0xab, 0xfc, 0xe0, 0xb1, 0xdf, 0xd5,
   0xcd, 0x95, 0x08, 0x09, 0x6d, 0x5b,
   0x2b, 0x8b, 0x6d, 0xf5, 0xd6, 0x71,
   0xef, 0x63, 0x77, 0xc0, 0x92, 0x1c,
   0xb2, 0x3c, 0x27, 0x0a, 0x70, 0xe2,
   0x59, 0x8e, 0x6f, 0xf8, 0x9d, 0x19,
   0xf1, 0x05, 0xac, 0xc2, 0xd3, 0xf0,
   0xcb, 0x35, 0xf2, 0x92, 0x80, 0xe1,
   0x38, 0x6b, 0x6f, 0x64, 0xc4, 0xef,
   0x22, 0xe1, 0xe1, 0xf2, 0x0d, 0x0c,
   0xe8, 0xcf, 0xfb, 0x22, 0x49, 0xbd,
   0x9a, 0x21, 0x37, 0x02, 0x03, 0x01,
   0x00, 0x01, 0x02, 0x81, 0x80, 0x33,
   0xa5, 0x04, 0x2a, 0x90, 0xb2, 0x7d,
   0x4f, 0x54, 0x51, 0xca, 0x9b, 0xbb,
   0xd0, 0xb4, 0x47, 0x71, 0xa1, 0x01,
   0xaf, 0x88, 0x43, 0x40, 0xae, 0xf9,
   0x88, 0x5f, 0x2a, 0x4b, 0xbe, 0x92,
   0xe8, 0x94, 0xa7, 0x24, 0xac, 0x3c,
   0x56, 0x8c, 0x8f, 0x97, 0x85, 0x3a,
   0xd0, 0x7c, 0x02, 0x66, 0xc8, 0xc6,
   0xa3, 0xca, 0x09, 0x29, 0xf1, 0xe8,
   0xf1, 0x12, 0x31, 0x88, 0x44, 0x29,
   0xfc, 0x4d, 0x9a, 0xe5, 0x5f, 0xee,
   0x89, 0x6a, 0x10, 0xce, 0x70, 0x7c,
   0x3e, 0xd7, 0xe7, 0x34, 0xe4, 0x47,
   0x27, 0xa3, 0x95, 0x74, 0x50, 0x1a,
   0x53, 0x26, 0x83, 0x10, 0x9c, 0x2a,
   0xba, 0xca, 0xba, 0x28, 0x3c, 0x31,
   0xb4, 0xbd, 0x2f, 0x53, 0xc3, 0xee,
   0x37, 0xe3, 0x52, 0xce, 0xe3, 0x4f,
   0x9e, 0x50, 0x3b, 0xd8, 0x0c, 0x06,
   0x22, 0xad, 0x79, 0xc6, 0xdc, 0xee,
   0x88, 0x35, 0x47, 0xc6, 0xa3, 0xb3,
   0x25, 0x02, 0x41, 0x00, 0xe7, 0xe8,
   0x94, 0x27, 0x20, 0xa8, 0x77, 0x51,
   0x72, 0x73, 0xa3, 0x56, 0x05, 0x3e,
   0xa2, 0xa1, 0xbc, 0x0c, 0x94, 0xaa,
   0x72, 0xd5, 0x5c, 0x6e, 0x86, 0x29,
   0x6b, 0x2d, 0xfc, 0x96, 0x79, 0x48,
   0xc0, 0xa7, 0x2c, 0xbc, 0xcc, 0xa7,
   0xea, 0xcb, 0x35, 0x70, 0x6e, 0x09,
   0xa1, 0xdf, 0x55, 0xa1, 0x53, 0x5b,
   0xd9, 0xb3, 0xcc, 0x34, 0x16, 0x0b,
   0x3b, 0x6d, 0xcd, 0x3e, 0xda, 0x8e,
   0x64, 0x43, 0x02, 0x41, 0x00, 0xb6,
   0x9d, 0xca, 0x1c, 0xf7, 0xd4, 0xd7,
   0xec, 0x81, 0xe7, 0x5b, 0x90, 0xfc,
   0xca, 0x87, 0x4a, 0xbc, 0xde, 0x12,
   0x3f, 0xd2, 0x70, 0x01, 0x80, 0xaa,
   0x90, 0x47, 0x9b, 0x6e, 0x48, 0xde,
   0x8d, 0x67, 0xed, 0x24, 0xf9, 0xf1,
   0x9d, 0x85, 0xba, 0x27, 0x58, 0x74,
   0xf5, 0x42, 0xcd, 0x20, 0xdc, 0x72,
   0x3e, 0x69, 0x63, 0x36, 0x4a, 0x1f,
   0x94, 0x25, 0x45, 0x2b, 0x26, 0x9a,
   0x67, 0x99, 0xfd, 0x02, 0x40, 0x28,
   0xfa, 0x13, 0x93, 0x86, 0x55, 0xbe,
   0x1f, 0x8a, 0x15, 0x9c, 0xba, 0xca,
   0x5a, 0x72, 0xea, 0x19, 0x0c, 0x30,
   0x08, 0x9e, 0x19, 0xcd, 0x27, 0x4a,
   0x55, 0x6f, 0x36, 0xc4, 0xf6, 0xe1,
   0x9f, 0x55, 0x4b, 0x34, 0xc0, 0x77,
   0x79, 0x04, 0x27, 0xbb, 0xdd, 0x8d,
   0xd3, 0xed, 0xe2, 0x44, 0x83, 0x28,
   0xf3, 0x85, 0xd8, 0x1b, 0x30, 0xe8,
   0xe4, 0x3b, 0x2f, 0xff, 0xa0, 0x27,
   0x86, 0x19, 0x79, 0x02, 0x40, 0x1a,
   0x8b, 0x38, 0xf3, 0x98, 0xfa, 0x71,
   0x20, 0x49, 0x89, 0x8d, 0x7f, 0xb7,
   0x9e, 0xe0, 0xa7, 0x76, 0x68, 0x79,
   0x12, 0x99, 0xcd, 0xfa, 0x09, 0xef,
   0xc0, 0xe5, 0x07, 0xac, 0xb2, 0x1e,
   0xd7, 0x43, 0x01, 0xef, 0x5b, 0xfd,
   0x48, 0xbe, 0x45, 0x5e, 0xae, 0xb6,
   0xe1, 0x67, 0x82, 0x55, 0x82, 0x75,
   0x80, 0xa8, 0xe4, 0xe8, 0xe1, 0x41,
   0x51, 0xd1, 0x51, 0x0a, 0x82, 0xa3,
   0xf2, 0xe7, 0x29, 0x02, 0x40, 0x27,
   0x15, 0x6a, 0xba, 0x41, 0x26, 0xd2,
   0x4a, 0x81, 0xf3, 0xa5, 0x28, 0xcb,
   0xfb, 0x27, 0xf5, 0x68, 0x86, 0xf8,
   0x40, 0xa9, 0xf6, 0xe8, 0x6e, 0x17,
   0xa4, 0x4b, 0x94, 0xfe, 0x93, 0x19,
   0x58, 0x4b, 0x8e, 0x22, 0xfd, 0xde,
   0x1e, 0x5a, 0x2e, 0x3b, 0xd8, 0xaa,
   0x5b, 0xa8, 0xd8, 0x58, 0x41, 0x94,
   0xeb, 0x21, 0x90, 0xac, 0xf8, 0x32,
   0xb8, 0x47, 0xf1, 0x3a, 0x3d, 0x24,
   0xa7, 0x9f, 0x4d
  ];

  function testNothing() {
  }

  // DISABLED until Chrome makes web crypto public.
  function DISABLEDtestRsaEs() {
    var res1 = new e2e.async.Result;
    var rsa1=new e2e.cipher.RSA('RSA');
    rsa1.setKey(RSA_KEY);
    var res2 = new e2e.async.Result;
    var rsa2=new e2e.cipher.RSA('RSA');
    rsa2.setKey(RSA_KEY);
    crypto.subtle.importKey(
      'spki',
      new Uint8Array(RSA_PUB_DER),
      {name: 'RSAES-PKCS1-v1_5'},
      false,
      ['encrypt']).then(function(k){
        rsa1.getWebCryptoKey=function(){return k};
        rsaes1 = new e2e.scheme.Rsaes(rsa1);
        res1.callback(1);
    });
    crypto.subtle.importKey(
      'pkcs8',
      new Uint8Array(RSA_SEC_DER),
      {name: 'RSAES-PKCS1-v1_5'},
      false,
      ['decrypt']).then(function(k){
        rsa2.getWebCryptoKey=function(){return k};
        rsaes2 = new e2e.scheme.Rsaes(rsa2);
        res2.callback(1);
    });
    var rsa3=new e2e.cipher.RSA('RSA');
    rsa3.setKey(RSA_KEY);
    rsaes3 = new e2e.scheme.Rsaes(rsa3);
    res1.addCallback(function() {
      res2.addCallback(function() {
        // rsa3 can encrypt and decrypt
        // rsa2 can decrypt
        // rsa1 can encrypt
        var PLAINTEXT = [1,2,3];
        rsaes1.encrypt(PLAINTEXT).addCallback(function(c) {
          rsaes2.decrypt(c).addCallback(function(p) {
            assertArrayEquals(PLAINTEXT, p);
            console.log('succ', 1, 2);
          });
          rsaes3.decrypt(c).addCallback(function(p) {
            assertArrayEquals(PLAINTEXT, p);
            console.log('succ', 1, 3);
          });
        });
        rsaes3.encrypt(PLAINTEXT).addCallback(function(c) {
          rsaes2.decrypt(c).addCallback(function(p) {
            assertArrayEquals(PLAINTEXT, p);
            console.log('succ', 3, 2);
          });
          rsaes3.decrypt(c).addCallback(function(p) {
            assertArrayEquals(PLAINTEXT, p);
            console.log('succ', 3, 3);
          });
        });
      });
    });
  }
</script>
